package io.gitee.zhangbinhub.admin.oauth.controller.openinner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.admin.common.api.CommonPath
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.common.base.BaseController
import io.gitee.zhangbinhub.admin.common.vo.InfoVo
import io.gitee.zhangbinhub.admin.oauth.service.UserService
import io.gitee.zhangbinhub.admin.resource.server.vo.UserVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.*
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.NotBlank
import org.noear.solon.validation.annotation.NotEmpty
import org.noear.solon.validation.annotation.NotNull
import org.noear.solon.validation.annotation.Valid

@Valid
@Controller
@Api("用户信息（内部开放接口）")
open class OpenInnerUserController(private val userService: UserService) : BaseController() {
    @ApiOperation("禁用指定用户")
    @Mapping(
        method = [MethodType.PATCH],
        value = CommonPath.openInnerBasePath + OauthApi.disableUser + "/{loginNo}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun disableUser(@Path(name = "loginNo") loginNo: String): InfoVo =
        userService.disableUser(loginNo).let { InfoVo("操作成功") }

    @ApiOperation("查询用户信息")
    @Mapping(
        method = [MethodType.POST],
        value = CommonPath.openInnerBasePath + OauthApi.userList,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    fun getUserListByIdList(
        @ApiParam("id列表", required = true)
        @NotEmpty(message = "id不能为空")
        @NotNull(message = "id不能为空")
        @Body
        idList: MutableList<String>
    ): List<UserVo> = userService.getUserVoListByIdList(idList)

    @ApiOperation("查询用户信息")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + OauthApi.userConfig + "/{userId}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun getUserInfoById(@Path(name = "userId") userId: String): UserVo =
        userService.getUserVoById(userId)

    @ApiOperation("查询用户信息")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + OauthApi.userConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun getUserInfoByLoginNo(@Param(name = "loginNo") loginNo: String): UserVo =
        userService.getUserVoByLoginNo(loginNo)

    @ApiOperation("通过机构编码和角色编码，查询用户列表")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + OauthApi.userList,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun getUserList(
        @ApiParam("当前用户登录号")
        @Param(name = "loginNo", required = false) loginNo: String?,
        @ApiParam("机构层级")
        @Param(name = "orgLevel", required = false) orgLevel: String?,
        @ApiParam("机构编码")
        @Param(name = "orgCode", required = false) orgCode: String?,
        @ApiParam("角色编码")
        @NotBlank(message = "角色编码不能为空")
        @Param(name = "roleCode", required = true) roleCode: String
    ): List<UserVo> = if (loginNo != null) {
        userService.getUserVoListByRelativeOrgAndRole(
            loginNo,
            orgLevel?.split(",")?.map { item -> item.toInt() } ?: listOf(0),
            roleCode.split(",")
        )
    } else if (orgCode != null) {
        userService.getUserVoListByOrgCodeAndRole(orgCode.split(","), roleCode.split(","))
    } else {
        userService.getUserVoListByRole(roleCode.split(","))
    }
}
